﻿using NPOI.HSSF.UserModel;
using OMG.Fill.Vegetable.Model.Ent;
using OMG.Fill.Vegetable.Service;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Http;
namespace OMG.Fill.Vegetable.Model.UI.Controllers
{
    public class VegBasicInfoController : ApiController
    {
        VegBasicInfoServices vegBasicInfoServices = new VegBasicInfoServices();
        //[ApiActionAuthorize("获取表1蔬菜基本情况调查T_VegBasicInfo分页列表对象操作")]
        //[ApiActionException]
        public Pager<List<VegBasicInfoEntity>> PostPageList(VegBasicInfoQuery query)
        {
            return vegBasicInfoServices.GetPageList(query);
        }
        //[ApiActionAuthorize("新增表1蔬菜基本情况调查T_VegBasicInfo指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Post(VegBasicInfoEntity model) {
            return vegBasicInfoServices.AddInfo(model);
        }
        //[ApiActionAuthorize("修改表1蔬菜基本情况调查T_VegBasicInfo指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Put(VegBasicInfoEntity model)
        {
            return vegBasicInfoServices.UpdateInfo(model);
        }
        //[ApiActionAuthorize("删除表1蔬菜基本情况调查T_VegBasicInfo指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Delete(string id)
        {
            return vegBasicInfoServices.DeleteInfo(id);
        }
        //[ApiActionAuthorize("获取表1蔬菜基本情况调查T_VegBasicInfo指定对象操作")]
        //[ApiActionException]
        public ResResult<VegBasicInfoEntity> Get(string id)
        {
            return vegBasicInfoServices.GetModel(id);
        }
        /// <summary>
        /// 获取本年和上一年的数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResResult<VegBasicInfoViewModel> PostData(VegNowModel model)
        {
            return vegBasicInfoServices.GetModel(model);
        }

        public ResResult<List<VegBasicInfoStatsModel>> PostDetailStats(VegNowModel model)
        {
            return vegBasicInfoServices.GetDetailStats(model);
        }

        /// <summary>
        /// 导出下载响应
        /// </summary>
        /// <param name="ReportCode"></param>
        public void GetExcelData(string ReportCode, string FillInYear, string AreaCode)
        {
            VegNowModel model = new VegNowModel();
            model.AreaCode = AreaCode;
            model.FillInYear = FillInYear;
            model.ReportCode = ReportCode;
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = model.FillInYear + "蔬菜用地情况表.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(vegBasicInfoServices.Method(model).GetBuffer());
            curContext.Response.End();
        }
        public ResResult<List<VegBasicInfoStatsModel>> PostStats(VegNowModel model)
        {
            return vegBasicInfoServices.GetStatsList(model);
        }
        public void GetStatsExcelData(string AreaCode, string FillInYear, string Status)
        {
            VegNowModel model = new VegNowModel();
            model.AreaCode = AreaCode;
            model.FillInYear = FillInYear;
            model.Status = Status;
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = model.FillInYear + "蔬菜用地情况统计表.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(vegBasicInfoServices.MethodStats(model).GetBuffer());
            curContext.Response.End();
        }
       
    }
}